package com.burgeon.backend.SHOPBACKEND.controller;

import com.burgeon.backend.SHOPBACKEND.service.WeCodeService;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;

@Controller
@CrossOrigin
@RequestMapping("code")
public class WeCodeController extends BaseController{

@Autowired
private WeCodeService weCodeService;


    /**
     * 创建临时带参二维码
     */
    @ResponseBody
    @RequestMapping(value = "/createCode")
    public ResponseResult<String> createCode(String daoId) throws ParseException {

        String url = weCodeService.getYJQrCode(daoId);

        return new ResponseResult<>(SUCCESS,url);
    }




//    public WeixinQRCode createTemporaryQRCode(String accessToken, int expireSeconds, String sceneStr) {
//        QRCodeParMapping byMappingValue = qrCodeParamMappingUtil.getQrcodeParMappingByMappingValue(sceneStr);
//        Integer mappingID = null;
//        if (byMappingValue == null) {
//            byMappingValue = new QRCodeParMapping();
//            byMappingValue.setMappingValue(sceneStr);
//            qrCodeParamMappingUtil.save(byMappingValue);
//        }
//        mappingID = byMappingValue.getMappingID();
//        sceneStr = mappingID.toString();
//        System.out.println("二维码携带参数为" + sceneStr);
//        WeixinQRCode weixinQRCode = null;
//        // 拼接请求地址
//        String requestUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=ACCESS_TOKEN";
//        requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken);
//        // 需要提交的json数据
//        String jsonMsg = "{\"expire_seconds\": %d, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": %s}}}";
//        // 创建临时带参二维码
//        JSONObject jsonObject = WechatUtil.httpsRequest(requestUrl, "POST",
//                String.format(jsonMsg, expireSeconds, sceneStr));
//
//        if (null != jsonObject) {
//            try {
//                weixinQRCode = new WeixinQRCode();
//                weixinQRCode.setTicket(jsonObject.getString("ticket"));
//                weixinQRCode.setExpireSeconds(jsonObject.getInt("expire_seconds"));
//                logger.info("创建临时带参二维码成功 ticket:{} expire_seconds:{}", weixinQRCode.getTicket(),
//                        weixinQRCode.getExpireSeconds());
//            } catch (Exception e) {
//                weixinQRCode = null;
//                int errorCode = jsonObject.getInt("errcode");
//                String errorMsg = jsonObject.getString("errmsg");
//                logger.error("创建临时带参二维码失败 errcode:{} errmsg:{}", errorCode, errorMsg);
//            }
//        }
//        return weixinQRCode;
//    }



}
